/*
 * InterfazGrafico.java
 */
package gui;

import cliente.*;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.*;

/**
 * 
 * @author Juan Jose Ferreiro Ferreiro
 */
public class InterfazGrafico extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3165619318448827210L;
	private static int porto = 25;
	private static String servidor = "localhost";

	/**
	 * Crea un obxecto da clase
	 */
	public InterfazGrafico() {
		initComponents();
	}

	/**
	 * Inicia o Frame
	 */
	private void initComponents() {

		botonEnviar = new javax.swing.JButton();
		botonCancelar = new javax.swing.JButton();
		etiquetaFrom = new javax.swing.JLabel();
		etiquetaTo = new javax.swing.JLabel();
		etiquetaCC = new javax.swing.JLabel();
		etiquetaAsunto = new javax.swing.JLabel();
		jTextFrom = new javax.swing.JTextField();
		jTextTo = new javax.swing.JTextField();
		jTextCC = new javax.swing.JTextField();
		jTextAsunto = new javax.swing.JTextField();
		etiquetaTexto = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jAreaTexto = new javax.swing.JTextArea();
		jLabelCabeceira = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Cliente SMTP");
		setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

		botonEnviar.setText("Enviar");
		botonEnviar.setToolTipText("Envia-lo correo");
		botonEnviar.addActionListener(new java.awt.event.ActionListener() {

			public void actionPerformed(java.awt.event.ActionEvent evt) {
				botonEnviarActionPerformed(evt);
			}
		});

		botonCancelar.setText("Cancelar");
		botonCancelar.addActionListener(new java.awt.event.ActionListener() {

			public void actionPerformed(java.awt.event.ActionEvent evt) {
				botonCancelarActionPerformed(evt);
			}
		});

		etiquetaFrom.setText("De:");

		etiquetaTo.setText("Para:");

		etiquetaCC.setText("CC:");

		etiquetaAsunto.setText("Asunto:");

		jTextFrom.setFont(jTextFrom.getFont());
		// jTextFrom.setText("ola@ola.es");
		jTextFrom.setToolTipText("Remitente do correo");

		jTextTo.setFont(jTextTo.getFont());
		// jTextTo.setText("tic@practicasrc.com");
		jTextTo.setToolTipText("Destinatario(s) do correo");

		jTextCC.setFont(jTextCC.getFont());
		// jTextCC.setText("tac@practicasrc.com; toc@practicasrc.com");
		jTextCC.setToolTipText("Outros destinatarios do correo");

		jTextAsunto.setFont(jTextAsunto.getFont());
		// jTextAsunto.setText("ASUNTO");

		etiquetaTexto.setText("Texto:");

		jAreaTexto.setColumns(20);
		jAreaTexto.setFont(jAreaTexto.getFont());
		jAreaTexto.setLineWrap(true);
		jAreaTexto.setRows(5);
		// jTextTexto.setText("CORPO DO E-MAIL");
		jScrollPane1.setViewportView(jAreaTexto);

		jLabelCabeceira.setText("Introduza os datos para envia-la mensaxe");

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap().add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(etiquetaFrom).add(
								etiquetaTo).add(etiquetaCC).add(etiquetaAsunto).add(etiquetaTexto)).add(25, 25, 25)
						.add(
								layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
										jLabelCabeceira, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 491,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(
										layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false).add(
												jScrollPane1).add(jTextFrom,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(jTextTo,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
												.add(jTextCC).add(jTextAsunto).add(
														org.jdesktop.layout.GroupLayout.TRAILING,
														layout.createSequentialGroup().add(botonCancelar,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(18,
																18, 18).add(botonEnviar,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
						.addContainerGap(46, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				layout.createSequentialGroup().add(24, 24, 24).add(jLabelCabeceira).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED, 23, Short.MAX_VALUE).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(etiquetaFrom).add(
								jTextFrom, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(etiquetaTo).add(
								jTextTo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(etiquetaCC).add(
								jTextCC, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(etiquetaAsunto).add(
								jTextAsunto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(etiquetaTexto).add(
								jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 191,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).add(30, 30, 30).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(botonEnviar).add(
								botonCancelar)).add(22, 22, 22)));

		pack();
	}

	/**
	 * Accion que se realiza ao pulsa-lo boton Aceptar
	 * 
	 * @param evt
	 */
	private void botonEnviarActionPerformed(java.awt.event.ActionEvent evt) {

		String remitente, asunto, texto;
		String[] destinatario = null, cc;
		remitente = jTextFrom.getText();
		if (remitente.matches("")) {
			JOptionPane.showMessageDialog(this, "Debe incluir un remitente", "Erro", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		if(jTextTo.getText().indexOf(',') == -1)
			destinatario = jTextTo.getText().split(";");
		else
			destinatario = jTextTo.getText().split(",");
		
		if (destinatario[0].matches("")) {
			JOptionPane.showMessageDialog(this, "Debe incluir, polo menos, un destinatario", "Erro",
					JOptionPane.ERROR_MESSAGE);
			return;
		}

		if(jTextCC.getText().indexOf(',') == -1)
			cc = jTextCC.getText().split(";");
		else
			cc = jTextCC.getText().split(",");
		
		if (cc[0].matches("")) {
			cc = null;
		}

		asunto = jTextAsunto.getText();

		if (asunto.matches("")) {
			int opcion = JOptionPane.showConfirmDialog(this, "Desexa envia-la mensaxe sen asunto?", "Pregunta",
					JOptionPane.YES_NO_OPTION);
			switch (opcion) {
			case JOptionPane.YES_OPTION:
			case JOptionPane.CLOSED_OPTION:
				break;
			default:
				return;
			}
		}

		texto = jAreaTexto.getText();
		if (texto.matches("")) {
			int opcion = JOptionPane.showConfirmDialog(this, "Desexa envia-la mensaxe sen texto?", "Pregunta",
					JOptionPane.YES_NO_OPTION);
			switch (opcion) {
			case JOptionPane.YES_OPTION:
			case JOptionPane.CLOSED_OPTION:
				break;
			default:
				return;
			}
		}

		Correo correo = new Correo(remitente, destinatario, cc, asunto, texto);

		try {
			new Smtp(servidor, porto).enviar(correo);
		} catch (UnknownHostException e) {
			JOptionPane.showMessageDialog(this, "O host " + e.getMessage() + " e desconhecido", "Erro",
					JOptionPane.ERROR_MESSAGE);
			return;
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
			return;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
			return;
		}

		JOptionPane.showMessageDialog(this, "Mensaxe enviada");

		int opcion = JOptionPane.showConfirmDialog(this, "Desexa enviar outro correo?", "Pregunta",
				JOptionPane.YES_NO_OPTION);
		if (opcion == JOptionPane.YES_OPTION) {
			return;
		}

		System.exit(0);
	}

	/**
	 * Accion que se realiza ao pulsa-lo boton Cancelar
	 * 
	 * @param evt
	 */
	private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_botonCancelarActionPerformed
		System.exit(0);
	}
	
	/**
	 * Fai que se arranque o interfaz grafico
	 * 
	 * @param serv Maquina onde se executa o servidor SMTP
	 * @param p Porto no que escoita o servidor SMTP
	 */
	public static void arranca(String serv, int p) {
		servidor = serv;
		porto = p;
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new InterfazGrafico().setVisible(true);
			}
		});
	}

	// Declaracion de variables
	private javax.swing.JButton botonCancelar;
	private javax.swing.JButton botonEnviar;
	private javax.swing.JLabel etiquetaAsunto;
	private javax.swing.JLabel etiquetaCC;
	private javax.swing.JLabel etiquetaFrom;
	private javax.swing.JLabel etiquetaTexto;
	private javax.swing.JLabel etiquetaTo;
	private javax.swing.JLabel jLabelCabeceira;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextField jTextAsunto;
	private javax.swing.JTextField jTextCC;
	private javax.swing.JTextField jTextFrom;
	private javax.swing.JTextArea jAreaTexto;
	private javax.swing.JTextField jTextTo;
}
